package com.mdlaser.config.srpingdoc;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringdocConfig {

    @Value("${project.name:未知}")
    private String projectName;

    @Value("${project.description:未知}")
    private String projectDescription;

    @Value("${project.version:未知}")
    private String projectVersion;

    @Value("${project.author:未知}")
    private String author;

    @Bean
    @ConditionalOnMissingBean(OpenAPI.class)
    public OpenAPI createApi(){
        return new OpenAPI().info(getInfo());
    }

    // 创建元数据
    private Info getInfo(){
        return new Info()
                // 标题
                .title(projectName)
                // 简短描述
                .description(projectDescription)
                // 指向服务条款的URL地址，必须是URL地址格式
                .termsOfService("")
                // 公开的API的联系人信息
                .contact(createCon())
                // API文档的版本信息
                .version(projectVersion);
    }

    // 创建公开的API的联系人信息
    private Contact createCon(){
        return new Contact()
                .email("841913613")
                .name(author)
                .url("");
    }
}
